"use strict";

var _typeof =
  typeof Symbol === "function" && typeof Symbol.iterator === "symbol"
    ? function(obj) {
        return typeof obj;
      }
    : function(obj) {
        return obj &&
        typeof Symbol === "function" &&
        obj.constructor === Symbol &&
        obj !== Symbol.prototype
          ? "symbol"
          : typeof obj;
      };

var maxHaibaoSubstring = 5;
$(document).ready(function() {
  // 切换时清空推荐理由
  $("x-tuijian #tuijian").on("change", function(evt) {
    $(evt.target)
      .siblings("input[type='text']")
      .val(null);
  });

  // 自动选择一个海报的名字
  $("x-haibao #tuiguang_name_3")
    .prop("checked", true)
    .change();
  // 自定义名称
  $("x-haibao #tuiguang_name_5")
    .siblings("input[type='text']")
    .on("click", function(evt) {
      $(evt.target)
        .siblings("input[type='radio']")
        .prop("checked", true);
    });
  $("x-haibao #tuiguang_name_5")
    .siblings("input[type='text']")
    .on("keyup", function(evt) {
      $(evt.target)
        .siblings("input[type='radio']")
        .prop("checked", true);
      $(evt.target).change();
    });
  $("x-haibao #tuiguang_name_5")
    .siblings("input[type='text']")
    .on("change", function(evt) {
      var content = String($(evt.target).val()).substring(
        0,
        maxHaibaoSubstring
      );
      $("x-haibao .haibao_img .haibao_pro_name").text(content);
    });
  // 留空时自动填入  自定义名字 五字
  $("x-haibao #tuiguang_name_5")
    .siblings("input[type='text']")
    .on("blur", function(evt) {
      if (String($(evt.target).val()).replace(/[\s　]/g, "") === "") {
        $(evt.target)
          .val("预定义名称")
          .change();
      }
    });

  // 动态修改海报文字
  // 真实名称
  $("x-haibao #tuiguang_name_1")
    .closest("label")
    .on("click", function(evt) {
      var content = String($("input[name='litter_name']").val()).substring(
        0,
        maxHaibaoSubstring
      );
      if (content === "") {
        evt.stopPropagation();
        evt.preventDefault();
        alert("请先填写产品小名");
        $("input[name='litter_name']").focus();
        throw new Error("\u65E0\u6CD5\u83B7\u53D6\u4EA7\u54C1\u5C0F\u540D");
      }
      $("x-haibao .haibao_img .haibao_pro_name").text(content);
    });
  // 自定义名称
  $("x-haibao #tuiguang_name_2")
    .closest("label")
    .on("click", function(evt) {
      $("x-haibao .haibao_img .haibao_pro_name").text("自定义名称");
    });
  // 不显示名称
  $("x-haibao #tuiguang_name_3")
    .closest("label")
    .on("click", function(evt) {
      $("x-haibao .haibao_img .haibao_pro_name").text(null);
    });
  // 预定义名称
  $("x-haibao #tuiguang_name_5")
    .closest("label")
    .on("click", function(evt) {
      var content = String(
        $("x-haibao #tuiguang_name_5")
          .siblings("input[type='text']")
          .val()
      ).substring(0, 8);
      if (content === "") {
        $("x-haibao #tuiguang_name_5")
          .siblings("input[type='text']")
          .val("预定义名称")
          .change();
      }
      $("x-haibao #tuiguang_name_5")
        .siblings("input[type='text']")
        .change();
    });
  $("x-haibao #tuiguang_edu_miaosu").on("keyup", function(evt) {
    var content = String($(evt.target).val()).substring(0, maxHaibaoSubstring);
    $("x-haibao .haibao_img .lixi_content").text(content);
    //
  });
  $("x-haibao #tuiguang_adv_miaosu").on("keyup", function(evt) {
    var content = String($(evt.target).val()).substring(0, maxHaibaoSubstring);
    $("x-haibao .haibao_img .adv_content").text(content);
    //
  });
});

function submitADnHaibao() {
  var AjaxURL = void 0;
  var AjaxAddURL = String($("#post_ajax_add_goods_ad").val());
  var AjaxUpdataURL = String($("#post_ajax_update_goods_ad").val());
  var nameState = $("x-haibao input[name='tuiguang_chanpin']:checked").val();
  var specialName = String(
    $("x-haibao input[name='tuiguang_chanpin_zidingyi']").val()
  );
  var ajaxData = {
    // 单选处理0真实名称1商家定义2不显示名称3自定义名称
    goods_name_state: nameState,
    // 利息额度描述
    lx_ed_dep: String($("x-haibao #tuiguang_adv_miaosu").val()),
    // 优势描述
    adv_dep: String($("x-haibao #tuiguang_edu_miaosu").val()),
    // 商品gid
    g_id: location.href.match(/\/id\/(\d+)/)[1],
    // 商品名称
    goods_name: specialName,
    ad_img: $(".haibao_wrap img").attr("src")
  };
  if (String($("#x_haibao_id").val()) !== "null") {
    AjaxURL = AjaxUpdataURL;
    ajaxData.goods_ad_id = $("#x_haibao_id").val();
  } else {
    AjaxURL = AjaxAddURL;
  }
  $.ajax(AjaxURL, {
    method: "post",
    data: ajaxData
  })
    .fail(function(err) {
      alert("\u901A\u4FE1\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5");
      throw err;
    })
    .done(function(res) {
      var resData = res.data;
      var flag = String(res.code) === "2";
      if (flag && String(_typeof(res.data)).toLowerCase() === "array") {
        $.each(res.data, function(index, item) {
          if (String(item) === "false") flag = false;
        });
      }
      if (String(res.code) === "1" || flag) {
        $("#x_haibao_id").val(res.goods_ad_id);
      } else {
        layer.msg(
          "\u63D0\u4EA4\u63A8\u5E7F\u6570\u636E\u5931\u8D25\uFF0C\u670D\u52A1\u5668\u8FD4\u56DE\uFF1A" +
            res.data
        );
      }
    });
}

// 提交推荐选项
function submitTuijian() {
  var ID = String($("#x-tuijian-id").val());
  var addURL = String($("#post_ajax_add_goods_diy_name").val());
  var updateURL = String($("#post_ajax_update_goods_diy_name").val());
  var AjaxURL = "";
  var AjaxData = {
    g_id: location.href.match(/\/id\/(\d+)/)[1],
    state:
      String($("x-tuijian input[type='checkbox']").prop("checked")) === "true"
        ? 1
        : 0, // true
    goods_name_reason: $("x-tuijian input[name='tuijian_reason']").val()
  };
  if (ID === "null") {
    AjaxURL = addURL;
  } else {
    AjaxURL = updateURL;
    AjaxData.goods_diy_name_id = ID;
  }
  $.ajax(AjaxURL, {
    method: "post",
    data: AjaxData,
    beforeSend: function beforeSend() {
      layer.msg("\u6B63\u5728\u53D1\u9001\u63A8\u8350\u4FE1\u606F");
    }
  })
    .fail(function(err) {
      throw err;
    })
    .done(function(res) {
      if (
        String(res.code) === "1" ||
        (String(res.code) === "2" && String(res.data) !== "false")
      ) {
        if (res.goods_diy_name_id !== undefined) {
          $("#x-tuijian-id").val("" + res.goods_diy_name_id);
        }
      } else {
        layer.msg(
          "\u63A8\u8350\u6D88\u606F\u53D1\u9001\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5"
        );
        throw new Error(
          "\u63A8\u8350\u6D88\u606F\u53D1\u9001\u5931\u8D25\uFF1A" + res.message
        );
      }
    });
}

$(document).ready(function() {
  /*
     let altFormDataZhengxin = `[{"cinv_times_id":"15","zhengxin_check_if":"1","zhengxin_check_count_method":"\u4e2a\u4eba\u67e5\u8be2","zhengxin_check_count_cycle":"1","zhengxin_check_count_time":"18","cinv_times_color":"#000","g_id":"30","state":"1"}]`;
    let altFormDataZhengxiYuqi = `[{"cinv_see_id":"14","yuqi_check_if":"1","yuqi_check_count_cycle":"1","yuqi_check_count_method":"\u7d2f\u8ba1","yuqi_check_count_time":"2","yuqi_check_count_source":"3","g_id":"30","state":"1","cinv_see_color":"#000"}]`;
    let altformDataHaibao = `[{"goods_ad_id":"7","goods_name_state":"2","lx_ed_dep":"","adv_dep":"","time":"2018\u5e7401\u670823\u65e5 04:58:54","g_id":"30","ad_img":"https:\/\/ss0.bdstatic.com\/94oJfD_bAAcT8t7mm9GUKT-xh_\/timg?image&quality=100&size=b4000_4000&sec=1516530300&di=ab46c4aa763445f2366af83938b4cbb8&src=http:\/\/image.tianjimedia.com\/uploadImages\/2015\/209\/42\/6H182F668EP9.jpg","u_id":"3","goods_name":""}]`;
    // [3]是关联，后端自己渲染
    let altformDataTuijian = `[{"goods_diy_name_id":"7","g_id":"30","u_id":null,"state":"0","goods_name_reason":""}]`;
    window.altFormData = {
      altFormDataZhengxin: altFormDataZhengxin,
      altformDataHaibao: altformDataHaibao,
      altformDataTuijian: altformDataTuijian,
      altFormDataZhengxiYuqi: altFormDataZhengxiYuqi
    }
   */
  /* 推荐选项数据 */
  var altformDataTuijian = window.altFormData.altformDataTuijian;
  if (altformDataTuijian === "") {
    throw new Error(
      "\u505C\u6B62\u63A8\u8350\u9009\u9879\u56DE\u586B\uFF0C\u4E0D\u5B58\u5728\u56DE\u586B\u6570\u636E"
    );
  }
  try {
    altformDataTuijian = JSON.parse("" + altformDataTuijian)[0];
  } catch (err) {
    alert(
      "\u505C\u6B62\u63A8\u8350\u9009\u9879\u56DE\u586B\uFF0C\u56DE\u586B\u6570\u636E\u683C\u5F0F\u6709\u8BEF"
    );
    throw err;
  }
  // 回填推荐数据
  if (altformDataTuijian.state === "1")
    $("#tuijian")
      .click()
      .prop("checked", true)
      .change();
  $("input[name='tuijian_reason']").val(
    altformDataTuijian.goods_name_reason || ""
  );
  $("#x-tuijian-id").val(altformDataTuijian.goods_diy_name_id);
  /* 海报数据 */
  var altformDataHaibao = window.altFormData.altformDataHaibao;
  if (altformDataTuijian === "") {
    throw new Error(
      "\u505C\u6B62\u6D77\u62A5\u6570\u636E\u56DE\u586B\uFF0C\u4E0D\u5B58\u5728\u56DE\u586B\u6570\u636E"
    );
  }
  try {
    altformDataHaibao = JSON.parse("" + altformDataHaibao)[0];
  } catch (err) {
    alert(
      "\u505C\u6B62\u6D77\u62A5\u6570\u636E\u56DE\u586B\uFF0C\u56DE\u586B\u6570\u636E\u683C\u5F0F\u6709\u8BEF"
    );
    throw err;
  }
  // 回填海报数据
  $("x-haibao img").attr("src", altformDataHaibao.ad_img);
  $("x-haibao input[name='tuiguang_chanpin_zidingyi']")
    .val(altformDataHaibao.goods_name || "")
    .keyup()
    .change();
  $(
    "x-haibao input[type='radio'][value='" +
      altformDataHaibao.goods_name_state +
      "']"
  )
    .prop("checked", true)
    .change();
  $("x-haibao #tuiguang_edu_miaosu")
    .val(altformDataHaibao.lx_ed_dep || "")
    .keyup()
    .change();
  $("x-haibao #tuiguang_adv_miaosu")
    .val(altformDataHaibao.adv_dep || "")
    .keyup()
    .change();
  $("#x_haibao_id").val(altformDataHaibao.goods_ad_id);
});
